/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesis2.pucp.Util;

import java.util.Properties;  
import javax.mail.Message;  
import javax.mail.MessagingException;  
import javax.mail.Session;  
import javax.mail.Transport;  
import javax.mail.internet.InternetAddress;  
import javax.mail.internet.MimeMessage; 

/**
 *
 * @author Yanire
 */
public class Mail {
    private final static Properties properties = new Properties();  
      
    private static String host = "smtp.gmail.com";  
    private static String user = "lbanonf@gmail.com";  
    private static String password = "20080363";  
  
    private static Session session;  
  
    private static void init() {  
  
        properties.put("mail.smtp.host", host);  
        properties.put("mail.smtp.starttls.enable", "true");  
        properties.put("mail.smtp.port", 587);  
        properties.put("mail.smtp.mail.sender",user);  
        properties.put("mail.smtp.user", user);  
        properties.put("mail.smtp.auth", "true");  
  
        session = Session.getDefaultInstance(properties);  
    }  
  
    public static void sendEmail(String mailto, String text, String subject){   
        init();  
        try{  
            MimeMessage message = new MimeMessage(session);  
            message.setFrom(new InternetAddress(user));  
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailto));  
            message.setSubject(subject);  
            message.setText(text);  
            Transport t = session.getTransport("smtp");  
            t.connect(host, user, password);  
            t.sendMessage(message, message.getAllRecipients());  
            t.close();  
        }catch (MessagingException me){  
            System.out.println(me.toString());
                        //Aqui se deberia o mostrar un mensaje de error o en lugar  
                        //de no hacer nada con la excepcion, lanzarla para que el modulo  
                        //superior la capture y avise al usuario con un popup, por ejemplo.   
        }  
    }  
}
